//用户信息资料的全局使用设定

function ConvertJSONDateToJSDateObject(jsondate) {
	var date = new Date(parseInt(jsondate.replace("/Date(", "").replace(")/", ""), 10));
	return date;
}

//获取日期
function getDate(date) {
	var year = date.getFullYear();
	var month = date.getMonth() + 1;
	var day = date.getDate();
	return year + "-" + month + "-" + day;
}

//月份
function addMoth(d, m) {
	var ds = d.split('-'),
		_d = ds[2] - 0;
	var nextM = new Date(ds[0], ds[1] - 1 + m + 1, 0);
	var max = nextM.getDate();
	d = new Date(ds[0], ds[1] - 1 + m, _d > max ? max : _d);
	return d.toLocaleDateString().match(/\d+/g).join('-')
}

//前面补0
function PrefixInteger(num, length) {
	return(Array(length).join('0') + num).slice(-length);
}

//获取月份
function formatMonth(myDate) {
	var year = myDate.getFullYear(); //获取完整的年份(4位,1970-????)
	var month = myDate.getMonth() + 1; //获取当前月份(0-11,0代表1月)
	month = PrefixInteger(month, 2);
	return year + "-" + month;
}

//获取日期时间
function formatDate_long(now) {
	var year = now.getFullYear();
	var month = now.getMonth() + 1;
	var date = now.getDate();
	var hour = now.getHours();
	var minute = now.getMinutes();
	var second = now.getSeconds();
	return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
}

//获取日期时间
function getDateTime(date) {
	var year = date.getFullYear();
	var month = date.getMonth() + 1;
	var day = date.getDate();
	var hh = date.getHours();
	var mm = date.getMinutes();
	var ss = date.getSeconds();
	return year + "-" + month + "-" + day + " " + hh + ":" + mm + ":" + ss;
}

//获取url中的参数
function getUrlParam(name) {
	//构造一个含有目标参数的正则表达式对象 
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
	//匹配目标参数 
	var r = window.location.search.substr(1).match(reg);
	//alert(r); 
	//返回参数值 
	if(r !== null)
		return decodeURI(r[2]);
	return null;
}

//获取具体参数
function getQueryString(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
	var r = window.location.search.substr(1).match(reg);
	if(r != null)
		return unescape(r[2]);
	return null;
}

//获取当天的日期
function gettoday() {
	var myDate = new Date();
	var tmpDate = myDate.toLocaleDateString();
	tmpDate = tmpDate.replace(/\//g, "-");
	return myDate;
}
//获取当天的日期
function GetDateStr(AddDayCount) {
	var dd = new Date();
	dd.setDate(dd.getDate() + AddDayCount); //获取AddDayCount天后的日期 
	var y = dd.getFullYear();
	var m = dd.getMonth() + 1; //获取当前月份的日期 
	var d = dd.getDate();
	m = PrefixInteger(m, 2);
	d = PrefixInteger(d, 2);
	return y + "-" + m + "-" + d;
}

//进行色彩的变化与图标变化
function iis_rise(id_Obj, is_rise, is_pic) {

	if(is_pic == 0) {
		//三个方块的图片
		if(is_rise == "0") {
			$(id_Obj).attr("src", "img/down.png");
		} else {
			$(id_Obj).attr("src", "img/up.png");
		}
	} else if(is_pic == 1) {
		//三个方块的内容
		if(is_rise == "0") {
			$(id_Obj).removeClass("up");
			$(id_Obj).addClass("down");
		} else {
			$(id_Obj).removeClass("down");
			$(id_Obj).addClass("up");
		}
	} else {
		if(is_rise == "0") {
			$(id_Obj).removeClass('mui-content xiaoguo up').addClass('mui-content xiaoguo down');
		} else {
			$(id_Obj).removeClass('mui-content xiaoguo down').addClass('mui-content xiaoguo up');
		}
	}

}

/*增加下拉框*/
function PopAddPicker(position_json, pickerObj) {
	var picker = new mui.PopPicker();
	console.log(position_json);
	picker.setData(position_json);

	picker.show(function(SelectedItem) {
		mui(pickerObj)[0].innerHTML = SelectedItem[0].text;
		//获取首页信息-数据
		statistics_quota();
	})

}

//计算离它最近的最大的整数数值
function resMax(data_max) {
	var xMod = 0;
	var x = data_max.toString();
	var l = x.length;
	x = x.split("");
	x[0] = parseInt(x[0]) + 1;

	for(var i = 1; i < l; i++) {
		x[i] = '0';
	}

	var z = x.join("");

	xMod = parseInt(z);
	return xMod;
}

//计算离它最近的最大的整数数值
function retMax(data_max) {
	var t = 0;
	var xMod = 0;
	if(data_max < 0) {
		t = 1;
		data_max = -data_max;
	};
	var x = data_max.toString();

	var l = x.length;
	if((data_max % 10) == 0) {
		xMod = data_max;
	} else {
		if(l > 1) {
			//			var m = parseInt(x.substr(0, 1)) + 1;
			//			var h = "00000000000000000000";
			//			xMod = parseInt(m.toString() + h.substr(0, l - 1));
			var m = data_max % 10;
			xMod = data_max - m + 10;
			//console.log(data_max);
			//console.log(m);
		} else {
			if(data_max == 0) {
				xMod = 0;
			} else {
				xMod = 10;
			}

		}
	}

	(t == 1) ? xMod = xMod * (-1): xMod = xMod;
	return xMod;
}

function formashortdate(format) {
	var date = new Date(format); //如果date为13位不需要乘1000
	var Y = date.getFullYear() + '-';
	var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
	var D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' ';
	var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
	var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()); //+ ':';
	var s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
	return Y + M + D + h + m;
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function new_databian(jiajie, obj_name) {
	var dateValue = mui(obj_name)[0].innerHTML;
	if(jiajie == 'jie') {
		dateValue = datebian(-1, dateValue);
	} else {
		dateValue = datebian(+1, dateValue);
	}
	//修补了日期向后超过当天的漏洞
	if(dateValue > GetDateStr(0)) {
		dateValue = GetDateStr(0);
	}

	mui(obj_name)[0].innerHTML = dateValue;

	user_info.look_start_date = dateValue; //开始搜索日期
	user_info.look_end_date = dateValue; //结束搜索日期		
}

//将用户选择的门店标签记录在案，然后进行本地存储
function shop_value_select(sou_text, sou_textid) {

	//修改每次获取的token值
	user_info.look_shop_id = sou_textid;
	user_info.look_shop_name = sou_text;
	//将存储成 json对象
	window.localStorage.setItem("user_info", JSON.stringify(user_info));

	return true;
}

//增加门店列表
function new_shoplistadd(obj_name) {
	//	var shop_json = [];
	//	mui.each(user_info.shop_list, function(index, item) {
	//		var xjson = {};
	//		xjson.value = user_info.shop_list[index].id;
	//		xjson.text = user_info.shop_list[index].shop_name;
	//		shop_json[index] = xjson;
	//	});
	//	var xjson = {};
	//	xjson.value = 999999;
	//	xjson.text = '添加门店';
	//	shop_json.push(xjson);
	//	obj_name.setData(shop_json);
	//console.log(shop_json);

	//add 添加门店 lists
	$("#shoplist_new").html("");
	var shop_json = [];
	mui.each(user_info.shop_list, function(index, item) {
		var xjson = {};
		xjson.value = user_info.shop_list[index].id;
		xjson.text = user_info.shop_list[index].shop_name;
		shop_json[index] = xjson;

		str = '<span class="xiala_span xiala_span1" data-shopid="' + xjson.value + '">' + xjson.text + '</span>';
		$("#shoplist_new").append(str);
	});
	var xjson = {};
	str = '<span class="add_span xiala_span1" data-shopid="999999"><img src="img/add.png" alt=""/>添加门店</span>';
	$("#shoplist_new").append(str);
	shop_json.push(xjson);

	//福利事件
	$(".xiala_span1").on("click", function() {
		var xSelectedItem = $(this).data("shopid");
		var nSelectedItem = $(this).html();
		//alert(xSelectedItem);

		//判断用户是绑定门店
		if(xSelectedItem == "999999") {
			window.location.href = "me_user_bind.html";
			return;
		}

		//登录成功，将用户信息进行本地储存
		shop_value_select(nSelectedItem, xSelectedItem);

		//变更显示的门店标签
		mui("#list-shop-lists")[0].innerHTML = user_info.look_shop_name;
		mui("#index-shop-lists-id")[0].value = user_info.look_shop_id;
		if (mui("#mine-shop")[0]) {
			mui("#mine-shop")[0].innerHTML = user_info.look_shop_name;
		}
		

		//
		$(".yinying").hide();
		$(".xialakuang").hide();

		//判断当前的href
		var thref = window.location.href;
		if(thref.indexOf('index.html') > -1) {
			//获取首页信息-数据
			main_data_list(); //--------------------------------------------------->>>>>>>其他页面换方法		
		} else if(thref.indexOf('client_searchresult.html') > -1) {
			if($("#active5-khfx").hasClass("active5")) {
				member_dashboard();
			} else {
				member_lists();
			}
		}

	});

}

//如果用户第一次进来，未选择门店
function new_firstcomeshop(obj_name) {
	//if(Object.getOwnPropertyNames(user_info.look) == 0) {
	if(user_info.look_shop_id == "data.lookshop_id") {
		mui(obj_name)[0].innerHTML = user_info.shop_list[0].shop_name;
		user_info.look_shop_id = user_info.shop_list[0].id;
		user_info.look_shop_name = user_info.shop_list[0].shop_name;
	} else {
		mui(obj_name)[0].innerHTML = user_info.look_shop_name;
	}
}

//验证密码
function testpass(str1) {
	//定义验证规则，由于字符串数字和字母的顺序可能不同。
	//也有可能字母和数字中间还包含了其他字符。故将验证规则分开定义。
	var regNumber = /\d+/; //验证0-9的任意数字最少出现1次。
	var regString = /[a-zA-Z]+/; //验证大小写26个字母任意字母最少出现1次。

	//验证第一个字符串
	if(regNumber.test(str1) && regString.test(str1)) {
		console.log('密码验证成功');
		return true;
	} else {
		mui.alert('您的密码必须包含字母与数字组合');
		return false;
	}
}

//获取URL参数
function getQueryString(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
	var reg_rewrite = new RegExp("(^|/)" + name + "/([^/]*)(/|$)", "i");
	var r = window.location.search.substr(1).match(reg);
	var q = window.location.pathname.substr(1).match(reg_rewrite);
	if (r != null) {
		return unescape(r[2]);
	} else if (q != null) {
		return unescape(q[2]);
	} else {
		return null;
	}
}
//文本换行
function stringAutoLine(params) {
	if (params.length>7) {
		return ''
	}
	return params.replace(/.{8}(?!$)/g, function (a) { return a + '\n'})  
}